//
//  XGL_PersonalGamesView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/17.
//

import UIKit
import CollectionKit

class XGL_PersonalGamesView: UIView {
    private var collectionView: CollectionView!
    private var gamesDatas:  ArrayDataSource<String> = ArrayDataSource(data: [])
    private var emptyText: UILabel!
    private var bottomLine: UIView!
    
    var games: [String] = []{
        didSet{
            if games.isEmpty{
                self.collectionView.isHidden = true
                self.emptyText.isHidden = false
                self.bottomLine.snp.remakeConstraints { make in
                    make.width.centerX.equalToSuperview()
                    make.height.equalTo(0)
                    make.bottom.equalTo(self.emptyText.snp.bottom)
                }
            }else{
                self.reloadDatas()
                self.collectionView.isHidden = false
                self.emptyText.isHidden = true
                self.bottomLine.snp.remakeConstraints { make in
                    make.width.centerX.equalToSuperview()
                    make.height.equalTo(0)
                    make.bottom.equalTo(self.collectionView.snp.bottom)
                }
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        let titleText = UILabel().then { mae in
            mae.text = "Favorite Game"
            mae.textColor = .white
            mae.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
        }
        self.emptyText = UILabel().then({ make in
            make.text = "None"
            make.textColor = .white
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
        })
        self.collectionView = CollectionView().then({ make in
            make.isHidden = true
        })
        self.bottomLine = UIView()
        self.addSubview(titleText)
        self.addSubview(self.collectionView)
        self.addSubview(emptyText)
        self.addSubview(self.bottomLine)
        titleText.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalToSuperview()
            make.height.equalTo(20.0)
        }
        self.collectionView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleText.snp.bottom).offset(14.0)
            make.left.equalTo(7.0)
            make.height.equalTo(0)
        }
        self.emptyText.snp.makeConstraints { make in
            make.left.equalTo(titleText)
            make.top.equalTo(titleText.snp.bottom).offset(14.0)
        }
        bottomLine.snp.makeConstraints { make in
            make.width.centerX.equalToSuperview()
            make.height.equalTo(0)
            make.bottom.equalTo(self.emptyText.snp.bottom)
        }
        self.snp.makeConstraints { make in
            make.bottom.equalTo(bottomLine.snp.bottom)
        }
        self.setCollectionData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_PersonalGamesView{
    private func reloadDatas(){
        self.gamesDatas.data = self.games
        self.collectionView.layoutIfNeeded()
        self.layoutIfNeeded()
        let width = collectionView.width / 6.0
        let height = 48.0
        self.collectionView.snp.updateConstraints { make in
            make.height.equalTo(ceil(Double(self.games.count) / 6.0) * height)
        }
    }
    

    private func setCollectionData(){
        let viewSource = ClosureViewSource(viewUpdater: { [weak self] (view: UIView, data: String, index: Int) in
            guard let `self` = self else { return }
            view.backgroundColor = .clear
            view.removeSubviews()
            let iconView = UIImageView().then { make in
                if data == "1"{
                    make.image = imageWithName("icon_game_valorant")
                }else if data == "2"{
                    make.image = imageWithName("icon_game_lol")
                }else if data == "3"{
                    make.image = imageWithName("icon_game_overwatch")
                }else if data == "4"{
                    make.image = imageWithName("icon_game_wow")
                }
            }
            view.addSubview(iconView)
            iconView.snp.makeConstraints { make in
                make.center.equalToSuperview()
                make.width.height.equalTo(48.0)
                make.left.equalTo(8.0)
            }
        })
        let sizeSource = { (index: Int, data: String, collectionSize: CGSize) -> CGSize in
            return CGSize(width: 64.0, height: 48.0)
        }
        let provider = BasicProvider(
            dataSource: self.gamesDatas,
            viewSource: viewSource,
            sizeSource: sizeSource
        )
        self.collectionView.provider = provider
    }

}
